﻿//-------------------------------------------------------------------------------------------
// <copyright file="ActionBarViewModel.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.TestClient.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;

    /// <summary>
    /// TODO: Describe the class
    /// </summary>
    public class ActionBarViewModel : ViewModel<INotifyPropertyChanged>
    {
        public static readonly DependencyProperty AccountIdProperty = DependencyProperty.Register("AccountId", typeof(string), typeof(ActionBarViewModel), new PropertyMetadata("r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"));
        private DelegateCommand loadCommand;
        private IRippleContext rippleContext;

        /// <summary>
        /// Initializes a new instance of the ActionBarViewModel class.
        /// </summary>
        public ActionBarViewModel(IRippleContext context)
        {
            this.rippleContext = context;
        }

        public event EventHandler AccountSelected;

        public event EventHandler NotFound;

        public string AccountId
        {
            get { return (string)GetValue(AccountIdProperty); }
            set { SetValue(AccountIdProperty, value); }
        }

        public DelegateCommand LoadCommand
        {
            get
            {
                if (this.loadCommand == null)
                {
                    this.loadCommand = new DelegateCommand(param => this.LoadCommandExecutor());
                }

                return this.loadCommand;
            }
        }

        private void LoadCommandExecutor()
        {
            if (this.rippleContext.Accounts.Get(this.AccountId) == null)
            {
                this.OnNotFound();
                return;
            }

            this.OnAccountSelected();
        }

        private void OnAccountSelected()
        {
            EventHandler handler = this.AccountSelected;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        private void OnNotFound()
        {
            EventHandler handler = this.NotFound;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
}